package com.panopset.nls;

import static com.panopset.Strings.*;
import static com.panopset.Util.*;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import javax.swing.ImageIcon;

import com.panopset.gui.SpritePackage;

/**
 * Flag icons available in flywheel.jar.
 *
 * @author Karl Dinwiddie
 *
 */
public final class FlagIcons {

    /**
     *
     * @return List of Flag resources, EN,DE et.
     */
    public static Vector<String> getResourceList() {
        return getInstance().getSpritePackage().getResourceList();
    }

    /**
     * Get flag for a particular locale name.
     *
     * @param localeName
     *            Locale name for flag.
     * @return Flag of provided locale name.
     */
    public static ImageIcon getFlag(final String localeName) {
        return getInstance().getFlags().get(localeName);
    }

    /**
     * Check to see if an NLS code has a corresponding flag icon.
     *
     * @param nlsCode
     *            , NLS code, ie: en, fr, de etc.
     * @return true if there is a flag icon available for nlsCode
     */
    public static boolean isValidNlsCode(final String nlsCode) {
        if (!isPopulated(nlsCode)) {
            return false;
        }
        if (getInstance().getSpritePackage().getResourceList()
                .contains(nlsCode)) {
            return true;
        } else {
            log("Flag icon not found for " + nlsCode);
            return false;
        }
    }

    /**
     * Sprite package.
     */
    private SpritePackage spritePkg;

    /**
     * @return SpritePackage of flag icons.
     */
    protected SpritePackage getSpritePackage() {
        if (spritePkg == null) {
            spritePkg = new SpritePackage(ICNS_JAR);
        }
        return spritePkg;
    }

    /**
     * Flag icons jar path.
     */
    private static final String ICNS_JAR = "/com/panopset/nls/flagicons.jar";

    /**
     * Jar path.
     */
    private static final String JAR_PATH = PANSOPSET_USER_DIR + PATH_SEP
            + "flagIcons";

    /**
     * Flags map.
     */
    private Map<String, ImageIcon> flags;

    /**
     * @return Map of all flags.
     */
    private Map<String, ImageIcon> getFlags() {
        if (flags == null) {
            flags = new HashMap<String, ImageIcon>();
            String d = JAR_PATH + PATH_SEP;
            for (String ln : getSpritePackage().getResourceList()) {
                flags.put(ln, new ImageIcon(d + ln + ".png"));
            }
        }
        return flags;
    }

    /**
     * Check to see if an NLS properties file has a corresponding flag icon.
     *
     * @param propertiesFile
     *            Properties file, ie: en.properties.
     * @return true if there is a flag icon available for propertiesFile.
     */
    public static boolean isValidProperiesFile(final File propertiesFile) {
        if (propertiesFile == null) {
            return false;
        }
        if (propertiesFile.isDirectory()) {
            return false;
        }
        return isValidNlsCode(dropExtension(propertiesFile.getName()));
    }

    /**
     * Based on SingletonHolder inner class by Bill Pugh.
     *
     * <h5>References</h5>
     * <ul>
     * <li>
     * <a href="http://en.wikipedia.org/wiki/Singleton_pattern">
     * http://en.wikipedia.org/wiki/Singleton_pattern </a></li>
     * </ul>
     *
     */
    private static final class Singleton {
        /**
         * Instance variable.
         */
        private static final FlagIcons INSTANCE = new FlagIcons();

        /**
         * Private constructor.
         */
        private Singleton() {
            // Prevent instantiation.
        }
    }

    /**
     * @return static FlagIcons instance.
     */
    protected static FlagIcons getInstance() {
        return Singleton.INSTANCE;
    }

    /**
     * Private singleton constructor.
     */
    private FlagIcons() {
        try {
            getSpritePackage().saveImagesToDirectory(JAR_PATH);
        } catch (IOException e) {
            log(e);
        }
    }
}
